Application: Support sandboxed session state changes
authorMatthias Clasen <mclasen@redhat.com>
Sun, 20 Jan 2019 16:29:04 +0000 (11:29 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 19 Feb 2019 05:56:31 +0000 (00:56 -0500)
React to the session-state in StateChanged signals from
the inhibit portal and call QueryEndResponse as appropriate.

gtk/gtkapplication-dbus.c
gtk/gtkapplicationprivate.h

index 25766aaae2ac75213e7809e101dc6cc53644512f..05a35cc945c2eb84e834392fa7299455dbf752b3 100644 (file)
@@ -194,6 +194,13 @@ screensaver_signal_session (GDBusProxy     *proxy,
   gtk_application_set_screensaver_active (application, active);
 }
 
+enum {
+  UNKNOWN   = 0,
+  RUNNING   = 1,
+  QUERY_END = 2,
+  ENDING    = 3
+};
+
 static void
 screensaver_signal_portal (GDBusConnection *connection,
                            const char       *sender_name,
@@ -203,16 +210,42 @@ screensaver_signal_portal (GDBusConnection *connection,
                            GVariant         *parameters,
                            gpointer          data)
 {
-  GtkApplication   *application = data;
+  GtkApplicationImplDBus *dbus = (GtkApplicationImplDBus *)data;
+  GtkApplication *application = data;
   gboolean active;
   GVariant *state;
+  guint32 session_state = UNKNOWN;
 
   if (!g_str_equal (signal_name, "StateChanged"))
     return;
 
   g_variant_get (parameters, "(o@a{sv})", NULL, &state);
   g_variant_lookup (state, "screensaver-active", "b", &active);
-  gtk_application_set_screensaver_active (application, active);
+  gtk_application_set_screensaver_active (dbus->impl.application, active);
+
+  g_variant_lookup (state, "session-state", "u", &session_state);
+  if (session_state != dbus->session_state)
+    {
+      dbus->session_state = session_state;
+
+      /* Note that we'll only ever get here if we get a session-state,
+       * in which case, the interface is new enough to have QueryEndResponse.
+       */
+      if (session_state == ENDING)
+        {
+          g_application_quit (G_APPLICATION (application));
+        }
+      else if (session_state == QUERY_END)
+        {
+          g_dbus_proxy_call (dbus->inhibit_proxy,
+                             "QueryEndResponse",
+                             g_variant_new ("(o)", dbus->session_id),
+                             G_DBUS_CALL_FLAGS_NONE,
+                             G_MAXINT,
+                             NULL,
+                             NULL, NULL);
+        }
+    }
 }
 
 static void
@@ -456,7 +489,7 @@ gtk_application_impl_dbus_startup (GtkApplicationImpl *impl,
                                                   NULL,
                                                   G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
                                                   screensaver_signal_portal,
-                                                  impl->application,
+                                                  dbus,
                                                   NULL);
           g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
           g_variant_builder_add (&opt_builder, "{sv}",
index 8f6cdb001deb6fb72194f26d19ce949b676c1856..24ef7c4a4021874fff36a79e1e64506b7810bc2a 100644 (file)
@@ -139,6 +139,7 @@ typedef struct
   GSList *inhibit_handles;
   guint            state_changed_handler;
   char *           session_id;
+  guint            session_state;
 } GtkApplicationImplDBus;
 
 typedef struct